/**
 * Created by user on 2018/11/30.
 */
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const ROOT_PATH = process.cwd()
const clientPath = path.join(ROOT_PATH, 'src/client')
const environment = process.env.ENV

module.exports = (env, argv) => {
  let config = {
    entry: path.join(clientPath, 'index.js'),
    output: {
      filename: 'js/bundle.[hash].js',
      path: path.join(clientPath, 'dist/static'),
      publicPath: '/'
    },
    resolve: {
      alias: {
        '@': clientPath,
        '@images': path.join(clientPath, 'images'),
        '@styles': path.join(clientPath, 'styles')
      }
    },
    module: {
      rules: [
        {
          test: /\.(png|jpg|gif)/,
          use: [
            {
              loader: 'url-loader',
              options: {
                limit: 1,
                outputPath: 'images'
              }
            }
          ]
        },
        {
          test: /\.js/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: ['env', 'stage-0', 'react']
            }
          }
        },
        {
          test: /\.less$/,
          exclude: /node_modules/,
          use: ExtractTextPlugin.extract({
            fallback: 'style-loader',
            use: [
              {
                loader: 'css-loader',
                options: {
                  minimize: true,
                  modules: true,
                  localIdentName: '[name]_[hash:base64:6]'
                }
              },
              {
                loader: 'postcss-loader',
                options: {
                  config: {
                    path: path.join(ROOT_PATH, 'postcss.config.js')
                  }
                }
              },
              {
                loader: 'less-loader'
              }
            ]
          })
        },
        {
          test: /\.css$/,
          exclude: /node_modules/,
          use: ExtractTextPlugin.extract({
            fallback: 'style-loader',
            use: [
              {
                loader: 'css-loader',
                options: {
                  minimize: true,
                  modules: true,
                  localIdentName: '[name]_[hash:base64:6]'
                }
              },
              {
                loader: 'postcss-loader',
                options: {
                  config: {
                    path: path.join(ROOT_PATH, 'postcss.config.js')
                  }
                }
              }
            ]
          })
        }
      ]
    },
    plugins: [
      new HtmlWebpackPlugin({
        template: path.join(clientPath, 'index.html'),
        title: 'fuck',
        filename: 'index.html',
        inject: true
      }),
      new ExtractTextPlugin('css/app.css')
    ],
    devServer: {
      hot: true,
      inline: true,
      contentBase: path.join(clientPath, "dist"),
      port: 7000
    }
  }
  return config
}
